package com.ruoyi.auth.controller;


import com.ruoyi.auth.form.WxLoginBody;
import com.ruoyi.auth.service.MiniappLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信小程序用户接口
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@RestController
@RequestMapping("/wx/")
public class WxMaUserController {
    @Autowired
    private TokenService tokenService;
    //


    @Autowired
    private MiniappLoginService miniappLoginService;
     /**
     * 登陆接口
     */
    @PostMapping("login")
    public R<?> login(@RequestBody WxLoginBody wxLoginBody) {
        LoginUser wxUser =   miniappLoginService.login(wxLoginBody.getWxCode(), wxLoginBody.getToken(), wxLoginBody.getUsername(), wxLoginBody.getPassword());
      return R.ok(tokenService.MiniappCreateToken(wxUser));
    }





//    /**
//     * <pre>
//     * 获取用户信息接口
//     * </pre>
//     */
//    @GetMapping("/info")
//    public String info(@PathVariable String appid, String sessionKey,
//                       String signature, String rawData, String encryptedData, String iv) {
//        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
//
//        // 用户信息校验
//        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
//            return "user check failed";
//        }
//
//        // 解密用户信息
//        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
//
//        return JsonUtils.toJson(userInfo);
//    }
//

/**
 * <pre>
 * 获取用户绑定手机号信息
 * </pre>
 */
//    @PostMapping("/phone")
//    public R<?> phone(String Wxcode) throws WxErrorException {
//        List<WxMaProperties.Config> configs = this.wxMaProperties.getConfigs();
//        String appid = configs.get(0).getAppid();
//        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
//        String accessToken = wxService.getAccessToken();
//        System.out.println("access"+accessToken);
//        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("Wxcode", Wxcode);
//        HttpHeaders headers = new HttpHeaders();
//        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
//        RestTemplate restTemplate = new RestTemplate();
//        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);
//        return R.ok( response.getBody(),"获取手机号码成功.");
//    }
}
